package com.tiyende.credit.loop.ui.dialog

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.tiyende.credit.loop.R
import com.tiyende.credit.loop.TiyendeApp
import com.tiyende.credit.loop.common.CommonDialog
import com.tiyende.credit.loop.databinding.TiyendeDialogGuideLoanApplyBinding
import com.tiyende.credit.loop.event.TiyendeEventPointType
import com.tiyende.credit.loop.utils.TiyendeDensityUtil
import com.tiyende.credit.loop.utils.TiyendeTitleBarUtil
import com.tiyende.credit.loop.weight.pickerview.adapter.ArrayWheelAdapter
import com.tiyende.credit.loop.weight.wheelview.view.WheelView
import org.json.JSONObject

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/13 15:41
 * @Annotation：申贷引导dialog
 */
class TiyendeLoanApplyGuideDialog: CommonDialog {
    private lateinit var binding: TiyendeDialogGuideLoanApplyBinding
    private var mCallback: IDialogCallback? = null
    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    override fun initTiyendeWindow() {
        val params = window?.attributes
        params?.width = TiyendeApp.app.getPhoneWidth() - TiyendeDensityUtil.dp2px(32f)
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.dialog_scale_anim) //修改动画样式
        window?.setGravity(Gravity.TOP)
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getTiyendeRootView(): View {
        binding = TiyendeDialogGuideLoanApplyBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initTiyendeView() {
        val top = TiyendeTitleBarUtil.getStatusBarHeight()
        binding.applyGuideLayout.setPadding(0,top,0,0)
    }

    override fun initTiyendeListener() {
        setOnClickListener(binding.applyGuideLayout) //关闭
    }

    override fun loadTiyendeData() {
    }

    override fun onTiyendeViewClick(v: View?) {
        dismiss()
        mCallback?.onClick(v)
    }

    /**
     * 设置额度值
     */
    fun showTiyendeLoanApplyGuideDatas(data: JSONObject,amountList: ArrayList<String>?){
        val centerColors = intArrayOf(Color.parseColor("#ff2e9b95"), Color.parseColor("#ff2c366b"))
        val outColors = intArrayOf(Color.parseColor("#ffb0e1e2"), Color.parseColor("#ff329f97"))
        binding.wheelView.setTextShaderCenter(centerColors)
        binding.wheelView.setTextShaderOut(centerColors)
        binding.wheelView.setLineSpacingMultiplier(1.4f)
        binding.wheelView.setItemsVisibleCount(3)
        binding.wheelView.setAlphaGradient(true)
        binding.wheelView.setLabel("")
        binding.wheelView.setGravity(Gravity.CENTER)
        binding.wheelView.setTypefaceCenter(TiyendeApp.app.getBlackTypeface())
        binding.wheelView.setTypefaceOut(TiyendeApp.app.getMediumTypeface())
        binding.wheelView.setTypefaceLable(Typeface.createFromAsset(context.assets,"fonts/Roboto-Regular.ttf"))
        binding.wheelView.setDividerColor(Color.parseColor("#00000000"))
        binding.wheelView.setDividerType(WheelView.DividerType.WRAP)
        binding.wheelView.adapter = ArrayWheelAdapter(amountList)
        binding.wheelView.setCyclic(true)
        //设置单位
        binding.wheelView.setLabel(data.optString("currency"))
        binding.wheelView.currentItem = data.optInt("defaultIndex",0)
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(TiyendeEventPointType.DIALOG.TIYENDE_DIALOG_APPLY_GUIDE_SHOW)
    }

    override fun dismiss() {
        super.dismiss()
        addEventPointRequestAction(TiyendeEventPointType.DIALOG.TIYENDE_DIALOG_APPLY_GUIDE_DISMISS)
    }
}